﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Modules_System_PostAdd : System.Web.UI.Page
{
    ISys_PostHandler postHandler = new Sys_PostManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private void dataBind()
    {
        DataTable dtStatus = new DataTable();
        dtStatus.Columns.Add("Value");
        dtStatus.Columns.Add("Name");
        DataRow drStatus;

        drStatus = dtStatus.NewRow();
        drStatus[0] = "1";
        drStatus[1] = Enums.GetNormalStatusName(Enums.NormalStatus.Vallid);
        dtStatus.Rows.Add(drStatus);

        drStatus = dtStatus.NewRow();
        drStatus[0] = "0";
        drStatus[1] = Enums.GetNormalStatusName(Enums.NormalStatus.Invalid);
        dtStatus.Rows.Add(drStatus);

        ddlStatus.DataSource = dtStatus;
        ddlStatus.DataTextField = "Name";
        ddlStatus.DataValueField = "Value";
        ddlStatus.DataBind();

        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                Sys_PostQuery query = postHandler.GetSys_PostQuery(ID);
                if (query != null)
                {
                    txtCode.Text = query.Code;
                    txtName.Text = query.Name;

                    ddlStatus.SelectedValue = query.Status.ToString();
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ("".Equals(txtCode.Text.Trim()))
        {
            Jscript.Alert("代码不能为空", this.Page);
            return;
        }
        if ("".Equals(txtName.Text.Trim()))
        {
            Jscript.Alert("名称不能为空", this.Page);
            return;
        }
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (isCodeExists(txtCode.Text.Trim(), int.Parse(Request["ID"])))
            {
                Jscript.Alert("代码已存在", this.Page);
                return;
            }
        }
        else
        {
            if (isCodeExists(txtCode.Text.Trim()))
            {
                Jscript.Alert("代码已存在", this.Page);
                return;
            }
        }

        Sys_PostQuery query = new Sys_PostQuery();
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            query = postHandler.GetSys_PostQuery(int.Parse(Request["ID"]));
        }
        query.Code = txtCode.Text.Trim();
        query.Name = txtName.Text.Trim();
        query.Status = int.Parse(ddlStatus.SelectedValue.ToString());
        query.Sys_User_ID = int.Parse(Session["CurrentUserID"].ToString());

        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (postHandler.UpdateSys_Post(query))
            {
                Jscript.Alert("更新成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
        else
        {
            if (postHandler.InsertSys_Post(query))
            {
                Jscript.Alert("添加成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
    }


    private bool isCodeExists(string code)
    {
        bool istrue = false;

        DataTable dt = postHandler.GetSys_PostList(" Code = @Code", new SqlParameter[] { new SqlParameter("@Code", code) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    private bool isCodeExists(string code, int postId)
    {
        bool istrue = false;

        DataTable dt = postHandler.GetSys_PostList("Code=@Code and ID!=@Id", new SqlParameter[] { new SqlParameter("@Code", code), new SqlParameter("@Id", postId.ToString()) });

        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

}
